/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.hva.web.mvcwebapp.doa;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import nl.hva.web.mvcwebapp.models.Chapter;
import nl.hva.web.mvcwebapp.models.User;
import nl.hva.web.mvcwebapp.models.Course;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Thijs
 */
public class ChapterDAO {

    public ChapterDAO() {
    }

    public static List<Chapter> getAllChapters(int course_id) {
        
        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Chapter.class);
        SessionFactory factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        Query queryResult = session.createQuery("FROM Chapter WHERE course_courseid = '"+course_id+"'");
        
        List<Chapter> chapter_list = queryResult.list();

        
        
//        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
//        Transaction tx = session.beginTransaction();
//        List<Chapter> chapter_list = session.createCriteria(Chapter.class).list();
//        tx.commit();

        return chapter_list;
    }

    public static Chapter getChapter(int chapter_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Chapter chapter = (Chapter) session.get(Chapter.class, chapter_id);
        tx.commit();

        //Course course = new Course(1, 1, "Java", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendiere ante, id porttitor ipsum semper non. Etiam mollis euismod dapibus. Phasellus vel turpis sem. Vestibulum dui ...", "1", "http://www.downloadstad.nl/screenshot/Java.jpg");
        return chapter;
    }

    public static ArrayList insertChapter(HttpServletRequest request, Course course) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        
        Chapter chapter = getChaptersFromRequest(request, course);
        session.save(chapter);
        tx.commit();
        return validation_errors;
    }

    public static ArrayList updateChapter(HttpServletRequest request, Course course) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Chapter chapter = getChaptersFromRequest(request, course);
        session.update(chapter);
        tx.commit();
        return validation_errors;
    }

    public static void deleteChapter(int chapter_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Chapter tempChapter = (Chapter) session.load(Chapter.class, chapter_id);
        session.delete(tempChapter);
        tx.commit();
    }

    private static Chapter getChaptersFromRequest(HttpServletRequest request, Course course) {
        Chapter c = new Chapter();

        if (request.getParameter("chapterid") != null && !request.getParameter("chapterid").isEmpty()) {
            c.setChapterid(Integer.parseInt(request.getParameter("chapterid")));
        }
        if (request.getParameter("chaptername") != null && !request.getParameter("chaptername").isEmpty()) {
            c.setChapterName(request.getParameter("chaptername"));
        }
        if (request.getParameter("chapterdescription") != null && !request.getParameter("chapterdescription").isEmpty()) {
            c.setChapterdescription(request.getParameter("chapterdescription"));
        }
        if (request.getParameter("chapterpicture") != null && !request.getParameter("chapterpicture").isEmpty()) {
            c.setChapterpicture(request.getParameter("chapterpicture"));
        }
        if (request.getParameter("chaptercontent") != null && !request.getParameter("chaptercontent").isEmpty()) {
            c.setChapterContent(request.getParameter("chaptercontent"));
        }
        if (course != null) {
            c.setCourse(course);
        }

        return c;
    }
}
